<template>
 <view>
	<view class="uni-padding-wrap uni-common-mt">
	  <view class="uni-title uni-common-mt">
	     </view>
	       <view>
	         <scroll-view  scroll-y="true" class="scroll-Y" @scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll">
	                    <navigator v-for="(item,index) in chatDataList" class="chat_data" :url="`/pages/index/chat/talk/talk?id=${item.withId}&name=${item.fromUserId==user.id?item.toUser:item.fromUser}`">
						   <view class="chat_view_pic">
	                            <image :lazy-load="true" class="chat_header_pic" :src="item.headPic==user.avatarUrl?item.headPic:user.avatarUrl"></image>
	                       </view>
	                     <view class="chat_info">
	                         <view class="chat_info_name">{{item.fromUserId==user.id?item.toUser:item.fromUser}}</view>
	                         <view class="chat_info_msg">
								 <text v-if="item.fromUserId == user.id&&item.read=='1'" style="color: #737373;">[已读]</text>
								 <text v-if="item.fromUserId == user.id&&item.read=='0'" style="color: #1E90FF;">[未读]</text>
								 <text>{{"undefined"==item.messages?"[图片]":undefined==item.messages?"":item.messages}}</text>
							 </view>
	                     </view>
	                      <text class="chat_time" v-if="item"></text>
	                      <text class="chat_time" v-else>{{item.sendTime.length>10?item.sendTime.substring(0, 10):item.sendTime}}</text>
	                    </navigator>
						
	                </scroll-view>
	            </view>
	        <!--   <view @tap="goTop" class="uni-link uni-center uni-common-mt">
	                点击这里返回顶部
	            </view> -->
	        </view>
	    </view>
</template>

<script>
	import http from '../../../common/request.js'
	import {openComDB,closeComDB,executeSQL,dbInit} from '../../../common/sqlite.js'
	export default {
		onLoad() {
			this.user=uni.getStorageSync('userKey');
			 this.isOpenDB();
			 this.getChatList()
			// this.initHome()
		},
		onShow() {
			this.user=uni.getStorageSync('userKey');
			this.getChatList()
			// this.initHome()
		},
		
		data() {
			return {
					chatDataList:[],
					user:uni.getStorageSync('userKey')
			 }
		},
		methods: {
			isOpenDB() {
			    var isOpen = plus.sqlite.isOpenDatabase({
			        name: 'ppchatmsg', //
			        path: '_doc/ppchatmsg.db' //
			    });
			     this.openDB(); //
			},
			openDB() {
			    //SQLite     
			    openComDB('ppchatmsg', '_doc/ppchatmsg.db', res => {
			         dbInit();
			    });
			},
			queryChatFromSqlite(){
			   let user=uni.getStorageSync('userKey')
			   var querySql = 'SELECT id,read,photoUrl,msg as messages,avatar,fromUser,toUser,fromUserId,toUserId, (fromUserId+toUserId) AS giveId, MAX( sendTime ) AS sendTime, username as profession, (CASE fromUserId  WHEN '+user.id+' THEN toUserId  ELSE fromUserId END ) AS withId FROM message  WHERE ( fromUserId = '+user.id+' OR toUserId ='+user.id+' )  GROUP BY giveId  ORDER BY sendTime DESC';
			   executeSQL('ppchatmsg',querySql , res => { 
					 this.chatDataList = res;
			    })
			
			},
			
			
			async getChatList() {
				//this.queryChatFromSqlite();
				this.chatDataList=[]
					const res = await http.request({
						url: '/message/list_message',
						method:'GET',
						data:{}
					})
					if(res.data.code==0){
						this.chatDataList = res.data.data
					}
			},
			upper: function(e) {
			            console.log(e)
			        },
			        lower: function(e) {
			            console.log(e)
			        },
			        scroll: function(e) {
			            console.log(e)
			            this.old.scrollTop = e.detail.scrollTop
			        },
			        goTop: function(e) {
			            this.scrollTop = this.old.scrollTop
			            this.$nextTick(function() {
			                this.scrollTop = 0
			            });
			            uni.showToast({
			                icon:"none",
			                title:"纵向滚动 scrollTop 值已被修改为 0"
			            })
			        }
		}
	}
</script>

<style>
.chat_data{
  border-radius: 5rpx;
  padding: 1%;
  margin-top: 2%;
  width: 90%;
  margin-left: 5%;
  margin-right: 5%;
  display: flex;
  justify-content: space-between;
}
.chat_header_pic{
  width: 110rpx;
  height: 110rpx;
  border-radius: 50%;
}


.chat_view_pic{
  width: 20%;
}

.chat_info{
  float: left;
  width: 60%;
  margin-top: 3%;
  flex-direction: column;
  display: flex;
}

.line{
  margin-top: 10rpx;
  height: 1rpx;
  width: 100%;
  background-color: #D8D8D8;
}

.navigator{
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #FFE1FF;
  border-radius: 15rpx;
  color: #666;
}

.search_input{
  width: 90%;
  margin: 0 auto;
  height: 90rpx;
  padding: 10rpx;
 
}
.chat_time{
  width: 20%;
  margin-top: 5%;
  font-size: xx-small;
}


.chat_info_name{
  font-size: small;
}
.chat_info_msg{
  font-size: x-small;
  color: darkkhaki;
}
</style>
